/*
Merge parent locations onto cleaned PSID data

Author: GA
Last Modified 7/18/2019
*/

clear all

//get parent ID values
do "$dofiles/PSID/00_psid_parent_readin_v1"
do "$dofiles/PSID/00_psid_parent_format_v1"

gen uniqid = ER30001*1000 + ER30002
gen mom_id = ER32009*1000 + ER32010
gen dad_id = ER32016*1000 + ER32017
keep *id
tempfile parent_ids
save `parent_ids'

//creation of base sample: individuals whose mothers were respondents in 
//first year of PSID
use "$scratch/psid_long", clear
merge m:1 uniqid using `parent_ids', nogen keep(match)

//crude generation of marital status, as I can't find a better alternative for the PSID
sort year fam relate
gen spouse = (relate == 2)
bys year fam: egen spouse_present = max(spouse)
gen marst = 0
replace marst = 1 if relate == 2
replace marst = 1 	if relate == 1 & spouse_present == 1
drop spouse_present spouse

//get location of mother 
preserve
keep uniqid year stfips health
ren health mom_health
ren uniqid mom_id
ren stfips stfips_mom
tempfile momloc
save `momloc'
restore

//merge
merge m:1 mom_id year using `momloc', keep(1 3) nogen

//location of father
preserve
keep uniqid year stfips health
ren health dad_health
ren uniqid dad_id
ren stfips stfips_dad
tempfile poploc
save `poploc'
restore

//merge
merge m:1 dad_id year using `poploc', keep(1 3) nogen

//check to see how often moms and dads are together
count if stfips_dad!=. & stfips_mom!=.
count if stfips_dad!=. & stfips_mom!=. & stfips_dad == stfips_mom //almost always! honestly, remarkably little geographic separation
save "$scratch/psid_long_parentloc", replace //save




//
